package cn.maihe.elg.operation.centers.guogu.channel.service;

import cn.maihe.elg.operation.centers.guogu.auth.GuoGuCenterAuthConfig;
import cn.maihe.elg.operation.centers.guogu.channel.GuoGuBaseChannelService;
import cn.maihe.elg.operation.centers.guogu.dto.GuoGuBaseRespDTO;
import cn.maihe.elg.operation.centers.guogu.dto.channel.req.GuoGuPaymentNotifyReq;
import cn.maihe.elg.operation.centers.guogu.enums.GuoGuChannelMethod;
import cn.maihe.elg.operation.centers.guogu.enums.GuoGuRespCodeEnum;
import cn.maihe.elg.operation.centers.supports.model.bo.NoticeGuaranteeApplyResultBo;
import cn.maihe.elg.operation.centers.supports.model.bo.NoticePayResultBo;
import cn.maihe.elg.operation.model.dto.InnerBaseResp;
import cn.maihe.elg.operation.model.enums.CenterNoEnum;
import cn.maihe.elg.operation.repository.entity.AcceptOrderInfoDO;
import cn.maihe.elg.operation.service.business.BidCustomerService;
import cn.maihe.elg.operation.service.order.AcceptOrderInfoService;
import cn.maihe.elg.operation.supports.system.ElgResourceService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import javax.annotation.Resource;

/**
 * @Description 支付结果通知
 * @Author SSX
 * @Date 2025/03/18
 */
@Service
@Slf4j
public class GuoGuPaymentNotifyChannelService extends GuoGuBaseChannelService<GuoGuPaymentNotifyReq, GuoGuBaseRespDTO> {

    @Resource
    private ElgResourceService elgResourceService;
    @Resource
    private AcceptOrderInfoService acceptOrderInfoService;
    @Resource
    private BidCustomerService bidCustomerService;

    @Override
    protected GuoGuChannelMethod methodEnum() {
        return GuoGuChannelMethod.payment_notify;
    }


    public InnerBaseResp<NoticeGuaranteeApplyResultBo> paymentNotify(String clientId, NoticePayResultBo bo) {
        AcceptOrderInfoDO acceptOrderInfo = acceptOrderInfoService.getByAcceptOrderNo(bo.getAcceptOrderNo());
        Assert.notNull(acceptOrderInfo, "查无此投保单号记录");

        //BidCustomerDO bidCustomer = bidCustomerService.selectById(acceptOrderInfo.getBidCustomerId());

        GuoGuPaymentNotifyReq reqDTO = new GuoGuPaymentNotifyReq();
        reqDTO.setTradeNo(acceptOrderInfo.getCenterOrderId());
        String payResult = bo.getPayResult();
        if("1".equals(payResult)){
            reqDTO.setResult("0");
        } else {
            reqDTO.setResult("-1");
        }
        reqDTO .setMsg(bo.getPayResultMsg());

        InnerBaseResp<NoticeGuaranteeApplyResultBo> innerBaseResp;


        CenterNoEnum centerNoEnum = this.getCenterNoEnumById(acceptOrderInfo.getCenterId());
        try {
            GuoGuBaseRespDTO chlBizRespData;
            GuoGuCenterAuthConfig authConfig = guoGuEpointCenterInfosConfig.getAuthConfig(centerNoEnum);
            if ("0".equals(authConfig.getCenterInfo().getSyncPayResult())) {  // 通过配置项 syncPayResult控制
                chlBizRespData = new GuoGuBaseRespDTO();
                chlBizRespData.setCode(GuoGuRespCodeEnum.SUCCESS.getRespCode());
                chlBizRespData.setMsg("已开启不推送支付结果到中心");
            } else {
                chlBizRespData = this.doRequest(clientId,centerNoEnum, reqDTO);
            }
            bo.setRespData(chlBizRespData);
            //code=10已回调成功作为成功处理 {"code":"10","message":"该保函已经成功回调过了"}
            if (GuoGuRespCodeEnum.isSuccess(chlBizRespData.getCode())) {
                innerBaseResp = InnerBaseResp.success(bo);
            } else {
                innerBaseResp = InnerBaseResp.error(bo, chlBizRespData.getMsg());
            }
        } catch (Exception e) {
            log.error("支付结果通知 >> [{}] 异常:{}", centerNoEnum.getCenterName(), e.getMessage(), e);
            innerBaseResp = InnerBaseResp.error("支付结果通知异常:" + e.getMessage());
        }

        return innerBaseResp;

    }
}
